package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository repository;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void save(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void update(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> listAll() {
        List<UserInfo> list = repository.findAll();
        return list;
    }

    @Override
    public boolean checkPhone(String phone) {
        return repository.findByPhone(phone) != null;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //生成随机的4位数的验证码
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4);
        //创建短信
        StringBuilder sb = new StringBuilder(80);
        sb.append("你注册的短信验证码为:").append(code).append(",请在").
                append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用");
        /*RestTemplate template = new RestTemplate();
        //参数1:短信接口url
        String url = "https://way.jd.com/chuangxin/dxjk?mobile={1}&content=【叩丁狼】{2}&appkey={3}";
        //参数2:请求接口玩之后响应数据封装类型
        String ret = template.getForObject(url, String.class, "13250327917", sb.toString(), "71b15af712ce2a027d3f77d962857799");
        System.out.println(ret);
        if(!ret.contains("Success")){
            throw new LogicException("短信发送失败");
        }*/
        //假装短信发送成功
        System.err.println(sb);
        //验证码缓存到redis中
        userInfoRedisService.sendVerifyCode(phone,code);
    }

    @Override
    public void regist(UserInfo userInfo,String rpassword, String verifyCode) {
        //判断手机号是否为空
        AssertUtil.hasLength(userInfo.getPhone(),"手机号不能为空");
        //判断用户名是否为空
        AssertUtil.hasLength(userInfo.getNickname(),"账号不能为空");
        //判断密码是否为空
        AssertUtil.hasLength(userInfo.getPassword(),"密码不能为空");
        //判断确认密码是否为空
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        //判断验证码是否为空
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //判断密码和确认密码是否相同
        AssertUtil.isEquals(userInfo.getPassword(),rpassword,"两次输入密码不一致");
        //校验手机号是否正确
        //校验手机是否唯一
        if(this.checkPhone(userInfo.getPhone())){
            throw new LogicException("账号已存在");
        }
        //获取到redis中的验证码
        String code = userInfoRedisService.getVerifyCode(userInfo.getPhone());
        if(code == null){
            throw new LogicException("验证码已过期");
        }
        //判断验证码是否相同
        AssertUtil.isEqualsIgnoreCase(code,verifyCode,"验证码错误");
        //都判断正确存入数据库
        userInfo.setEmail("");
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");
        userInfo.setState(UserInfo.STATE_NORMAL);
        this.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        //一般会在主要查询列添加索引,查询速度较快
        UserInfo user = repository.findByPhone(username);
        //如果用户为空,抛用户不存在异常
        if(user == null){
            throw new LogicException("用户不存在");
        }
        if(!user.getPassword().equals(password)){
            throw new LogicException("密码不正确");
        }
        return user;
    }

    @Override
    public List<UserInfo> findByCity(String keyword) {
        List<UserInfo> list = repository.findByCity(keyword);
        return list;
    }


}
